package cn.com.inossem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;
import cn.com.inossem.utils.Utils;

import com.foxykeep.datadroid.requestmanager.Request;

/**
 * 
 * 检索向导
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class JianJieActivity extends RequestActivity {

	ImageView back;
	TextView submit, textnum;
	EditText fabuedit;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);
		initView();

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {
		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);

		submit = (TextView) this.findViewById(R.id.submit);
		submit.setOnClickListener(this);

		textnum = (TextView) this.findViewById(R.id.textnum);
		fabuedit = (EditText) this.findViewById(R.id.fabuedit);
		fabuedit.setText(Utils.messageInfo.userInfo.getSummary());
		int number = fabuedit.length();
		textnum.setText(number + "/" + 200);
		fabuedit.addTextChangedListener(new TextWatcher() {
			private CharSequence temp;
			private int selectionStart;
			private int selectionEnd;
			private int num = 200;

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				temp = s;
				// System.out.println("s=" + s);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				int number = s.length();
				textnum.setText(number + "/" + num);
				selectionStart = fabuedit.getSelectionStart();
				selectionEnd = fabuedit.getSelectionEnd();
				// System.out.println("start="+selectionStart+",end="+selectionEnd);
				if (temp.length() > num) {
					s.delete(selectionStart - 1, selectionEnd);
					int tempSelection = selectionStart;
					fabuedit.setText(s);
					fabuedit.setSelection(tempSelection);// 设置光标在最后
				}
			}
		});

	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {

	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.jianjie_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.submit:
			launchRequest(WooRequestFactory.updatePersonal("summary", fabuedit
					.getText().toString()));
			Utils.messageInfo.userInfo
					.setSummary(fabuedit.getText().toString());
			finish();
			break;
		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			setResult(1);
			finish();
		default:
			break;
		}
	}
}
